#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

template<class T> void f(T)
{
    int jug; cin >> jug; jug--;
    vector<function<bool(const T&, const T&)>> cmp({
        [](const T& l, const T& r) {return l < r; },
        [](const T& l, const T& r) {return l > r; }
    });

    T cur;
    vector<T> arr;
    while (cin >> cur) arr.push_back(cur);
    int n = arr.size();
    auto adjustdown = [&](int i)
    {
        int c = i * 2 + 1;
        while(c < n){
            if (c + 1 < n && cmp[jug](arr[c], arr[c + 1]))
                c++;
            if (cmp[jug](arr[i], arr[c]))
                swap(arr[i], arr[c]);
            else 
                break;
            i = c;
            c = i * 2 + 1;
        }
    };

    for (int i = (n - 1 - 1) / 2; i >= 0; --i) adjustdown(i);
    
    for (int i = 0; i < arr.size(); ++i) {
        if (i) cout << ',';
        cout << arr[i];
    }

    for (int i = n - 1; i > 0; --i) {
        swap(arr[0], arr[i]);
        n--;
        adjustdown(0);

        cout << endl;
        for (int i = 0; i < arr.size(); ++i) {
            if (i) cout << ',';
            cout << arr[i];
        }
    }
}


int main()
{
    int t; cin >> t; (void)getchar();
    if (t < 0 || t > 3) cout << "err";
    else if (t == 0)
        f(int());
    else if (t == 1)
        f(double());
    else if (t == 2)
        f(char());
    else if (t == 3)
        f(string());
    return 0;
}

//         21
//       25  49 
//  25 93      62 72 
//8 37  6 54